home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Converters / Convert_TEXT / Source / CRLFToNeXTText.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  101 lines

  1. /***********************************************************************
  2. CRLF Converter class for Convert TEXT which converts between Mac and NeXT text.
  3. Copyright (C) 1993 David John Burrowes
  4.  
  5. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.
  6.  
  7. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  8.  
  9. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10.  
  11. The author, David John Burrowes, can be reached at:
  12.     davidjohn@kira.net.netcom.com
  13.     David John Burrowes
  14.     1926 Ivy #10
  15.     San Mateo, CA 94403-1367
  16. ***********************************************************************/
  17.  
  18.  
  19.  
  20. /*====================================================================
  21. This is the implementation file for the CRLFToNeXTText class.  Check the doc for NeXTToMacText.rtf to give you a suggestion of what this otherwise undocumented class is like..
  22.  
  23. NOTE: You may find that text doesn't line up properly unless you use the New Century Schoolbook Roman typeface, since this was created with it.
  24.  
  25. INFORMATION:
  26.     This is $Revision: 1.2 $ of this file
  27.     It was last modifiFed by $Author: death $ on $Date: 93/04/04 23:44:44 $
  28.      $Log:    NeXTToCRLFText.m,v $
  29. ====================================================================*/
  30.  
  31.  
  32. #import "CRLFToNeXTText.h"
  33. #import <memory.h>    // for memcpy
  34. #include <strings.h>
  35.  
  36.  
  37. @implementation CRLFToNeXTText
  38.  
  39. //
  40. //    Note: an init method isn't needed, nor is a ConvertCharacter.  The latter is no
  41. //    good because we only get 1 char at a time, so we'd never see a CFLF...
  42. //
  43.  
  44.  
  45.  
  46. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. //    Routine:        ConvertString:WithLength:
  48. //    Parameters:    a pointer to a string of data to be converted
  49. //                the length of the data the poiner poins to.
  50. //    Returns:        a poiner to a new block of data to be converted
  51. //    Stores:        the pointer we are returning
  52. //                the length of the new data
  53. //    Description:
  54. //        Hokay.  This routine will take a string that may have CRLF line endings in
  55. //        it, and effecively removes all the CR's so we end up with a nice NeXT text
  56. //        file.
  57. //    Bugs:
  58. //        we don't really check for errors anywhere (not taht there are many oportunities)...
  59. //    History
  60. //        93.07.05    djb    Created for 1.1
  61. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. - (Pointer) ConvertString: (Pointer) theData WithLength: (Integer) length
  63. {
  64.     Character*    source = (Character*) theData; // getting a better typed ptr.
  65.     Integer        index;
  66.     PositiveInteger    destindex = 0;
  67.     Character*    dest = (Character*) NewPointer(length+1); // worst case: nothing changes, and we add a null
  68.     
  69.     [self    ResetResults];
  70.     
  71.     for (index = 0; index < length; index++)
  72.     {
  73.         //
  74.         //    I hate these multiple logical conditions things.  I believe this says:
  75.         //    If we find a CRLF and we're before the very end of the string (that is,
  76.         //    where the LF would be hangin off the end) then negate this whole condition.
  77.         //    under those circumstances, we want to copy the character (otherwise, the
  78.         //    carriage return is part of the CRLF, and we want to not write it out.
  79.         //
  80.         if (! ((source[index] == '\r') && (source[index+1] == '\n') && (index < (length-1))))
  81.         {
  82.             dest[destindex] = source[index];
  83.             destindex++;
  84.         }
  85.     }
  86.     //
  87.     //    Put a null on, just for the heck of it. 
  88.     //
  89.     dest[destindex] = EndOfCString;
  90.     //
  91.     //    Store the result, and return.  (note that destindex always ends up pointing to
  92.     //    the next byte to be used, and thus is also a count of the total number of bytes
  93.     //    in the dest string.
  94.     //
  95.     [self    StorePointer: dest];
  96.     [self    PutPositiveInteger: destindex Into: SECOND_RESULT];
  97.     [self    StoreErrorCode: errOK AndText: "Nothing could go wrong (pathetic program)!"];
  98.     return dest;
  99. }
  100. @end
  101.